package com.lianzhihui.romeex.utils;

import com.lianzhihui.romeex.bean.user.WalletInfo;
import com.lianzhihui.romeex.config.AppConstants;
import com.lianzhihui.romeex.database.DataBaseConfig;
import com.lianzhihui.romeex.database.DataBaseManager;
import com.lianzhihui.romeex.database.data.Rm_Wallet;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import io.realm.RealmResults;

/**
 * Description: 用户数据管理
 * Author: RAMON
 * CreateDate: 2020/10/29 2:15 PM
 * UpdateUser:
 * UpdateDate:
 * UpdateRemark:
 * Version:
 */
public class UserUtils {

    /**
     * 设置当前用户address
     *
     * @param address
     */
    public static boolean setCurrentUserAddress(String address) {
        return PreferenceUtils.commitString("CURRENT_USER", address);
    }

    /**
     * 获取当前用户address
     */
    public static String getCurrentUserAddress() {
        return PreferenceUtils.getString("CURRENT_USER", "");
    }

    /**
     * 获取当前设置的（人民币，美元）
     *
     * @return
     */
    public static String getCurrentUserMoney() {
        return PreferenceUtils.getString(AppConstants.CURRENCY_TYPE_KEY, AppConstants.CNY);
    }

    /**
     * 设置当前地址的绑定交易所uid
     *
     * @return
     */
    public static void setCurrentUserExchangeUid(String str) {
        PreferenceUtils.applyString(getCurrentUserAddress() + "exchangeUid", str);
    }

    /**
     * 获取当前地址的绑定交易所uid
     *
     * @return
     */
    public static String getCurrentUserExchangeUid() {
        return PreferenceUtils.getString(getCurrentUserAddress() + "exchangeUid", "");
    }

    /**
     * 获取当前是否开启了指纹验证
     *
     * @return
     */
    public static boolean getCurrentFinger() {
        return PreferenceUtils.getBoolean(AppConstants.KEY_BIOMETRIC_SWITCH_ENABLE, false);
    }

    /**
     * 根据用户地址获取当前地址所保存的添加的地址列表
     *
     * @return
     */
    public static String getCurrentSaveAddressList() {
        return PreferenceUtils.getString(UserUtils.getCurrentUserAddress(), "");
    }

    /**
     * 保存当前用户的地址列表信息
     */
    public static void setCurrentSaveAddressList(String jsonStr) {
        PreferenceUtils.applyString(UserUtils.getCurrentUserAddress(), jsonStr);
    }

    /**
     * 保存当前用户是否已经绑定上级
     */
    public static void setCurrentOpenStatus(boolean flag) {
        PreferenceUtils.applyBoolean(AppConstants.USER_OPENSTATUS + getCurrentUserAddress(), flag);
    }

    /**
     * 获取当前用户是否已经绑定上级
     */
    public static boolean getCurrentOpenStatus() {
        return PreferenceUtils.getBoolean(AppConstants.USER_OPENSTATUS + getCurrentUserAddress(), false);
    }

    /**
     * 当前使用用户
     *
     * @return
     */
    public static WalletInfo getCurrentUser() {
        //获取配置文件中的 用户id
        String address = getCurrentUserAddress();
        //数据库查询该用户信息
        WalletInfo user = null;
        if (!address.isEmpty()) {
            user = DataBaseManager.getUser(address);
        }
        return user;
    }

    /**
     * 当前使用用户
     *
     * @return
     */
    public static WalletInfo getUser(String address) {
        //数据库查询该用户信息
        return DataBaseManager.getUser(address);
    }

    /**
     * 获取货币 类型
     *
     * @return 1 CNY，2 USD
     */
    public static int getCurrentyType() {
        String type = getCurrentUserMoney();
        int typeInt = 1;
        if (type.equals(AppConstants.CNY)) {
            typeInt = 1;
        }
        if (type.equals(AppConstants.USD)) {
            typeInt = 2;
        }
        return typeInt;
    }

    /**
     * 同步获取所有钱包
     *
     * @param reDir 是否重新排序
     * @return
     */
    public static List<WalletInfo> getAllUserList(boolean reDir) {
        RealmResults<Rm_Wallet> all = DataBaseManager.getAllUser();
        List<WalletInfo> allList = new ArrayList<>();
        int index = 0;
        for (int i = 0; i < all.size(); i++) {
            WalletInfo wi = DataBaseManager.db2Bean(all.get(i));
            if (UserUtils.getCurrentUserAddress().equals(wi.getAddress())) {
                index = i;
            }
            allList.add(wi);
        }
        if (reDir) {
            Collections.swap(allList, index, 0);
        }
        return allList;
    }

    /**
     * 同步获取所有钱包
     */
    public static List<WalletInfo> getAllUserList() {
        RealmResults<Rm_Wallet> all = DataBaseManager.getAllUser();
        List<WalletInfo> allList = new ArrayList<>();
        int index = 0;
        for (int i = 0; i < all.size(); i++) {
            WalletInfo wi = DataBaseManager.db2Bean(all.get(i));
            if (UserUtils.getCurrentUserAddress().equals(wi.getAddress())) {
                index = i;
            }
            allList.add(wi);
        }
        return allList;
    }

    /**
     * 保存可见状态
     *
     * @param isSee
     */
    public static void saveCanSee(boolean isSee) {
        PreferenceUtils.commitBoolean("cansee", isSee);
    }

    /**
     * 获取可见状态
     *
     * @return 是否可见
     */
    public static boolean getCanSee() {
        return PreferenceUtils.getBoolean("cansee", true);
    }

}
